import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageRedimMain {

	private BufferedImage bInput;
	private BufferedImage bOutput;
	private int wMargin;
	private int hMargin;

	public static void main(String[] args) throws Throwable {
		new ImageRedimMain().run(args);
	}

	private void run(String[] args) throws Throwable {
		File input = new File(args[0]);
		File output = new File(input.getParent(), "out_" + input.getName()
				+ ".png");
		bInput = ImageIO.read(input);
		calculateMargins();
		bOutput = drawBorders();
		ImageIO.write(bOutput, "png", output);
	}

	private void calculateMargins() throws IOException {
		int iWidth = bInput.getWidth();
		int iHeight = bInput.getHeight();
		wMargin = iWidth / 30;
		hMargin = iHeight / 30;
	}

	public BufferedImage drawBorders() {
		int oHeight = bInput.getHeight() + hMargin * 2;
		int oWidth = bInput.getWidth() + wMargin * 2;
		final BufferedImage bufferedImage = new BufferedImage(oWidth, oHeight,
				BufferedImage.TYPE_INT_RGB);
		final Graphics2D graphics2D = bufferedImage.createGraphics();
		graphics2D.setColor(Color.WHITE);
		graphics2D.fillRect(0, 0, oWidth, hMargin);
		graphics2D.fillRect(0, 0, wMargin, oHeight);
		graphics2D.fillRect(0, oHeight - hMargin, oWidth, hMargin);
		graphics2D.fillRect(oWidth - wMargin, 0, wMargin, oHeight);
		graphics2D.setComposite(AlphaComposite.Src);
		graphics2D.drawImage(bInput, wMargin, hMargin, bInput.getWidth(),
				bInput.getHeight(), null);
		graphics2D.dispose();

		return bufferedImage;
	}
}
